<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>promise.race</title>
</head>

<body>

</body>
<script>
    let p1 = new Promise(resolve => {
        setTimeout(() => {
            resolve("p1")
        }, 2000);
    })
    let p2 = new Promise(resolve => {
        setTimeout(() => {
            resolve("p2")
        }, 1000);
    })

    Promise._race = function (arr) {
        return new Promise(resolve => {
            for (let i = 0; i < arr.length; i++) {
                arr[i].then(res => {
                    resolve(res)
                })
            }
        })

    }
    
    Promise._race([p1, p2]).then(result => {
        console.log(result)
    })

    // Promise.race([p1, p2]).then(result => {
    //     console.log(result)
    // })
</script>

</html>